package com.financing.wap.web.controller.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.FaUserDTO;
import com.financing.wap.dto.ProjectCommentsDTO;
import com.financing.wap.dto.ProjectCommentsLinkDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IProjectCommentsService;
import com.financing.wap.service.IUserService;
import com.financing.wap.util.Constants;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("apiProductCommentsController")
@RequestMapping("/api/commentsProduct")
public class ProjectCommentsController  extends BaseController {
	@Autowired
	IProjectCommentsService projectCommentsService;
	@Autowired
	private IUserService userService;
	
	Logger logger = Logger.getLogger(getClass());

	@RequestMapping("/list/{pId}")
	@ResponseBody
	public ResultMap list(@PathVariable int pId,Pager pager,int _pId){
		logger.info("in product comments list begin...");
		ProjectCommentsDTO where = new ProjectCommentsDTO();
		if(_pId > 0){
			pId = _pId;
		}
		where.setpId(pId);
		
		Map<String,FaUserDTO> userMap = new HashMap<String,FaUserDTO>();
		ResultMap result = new ResultMap();
		try {
			List<ProjectCommentsDTO> commentsList  = projectCommentsService.getCommentsList(where,pager); 
			
			for(ProjectCommentsDTO comments : commentsList){
				String uid = comments.getuId();
				if (!userMap.containsKey(uid)){
					
					FaUserDTO user = userService.getUserById(uid);
					user.setuLoginName(StringUtil.encryptCardNumber(user.getuLoginName()));
					userMap.put(uid, user);
				}
				comments.setUser(userMap.get(uid));
			}
			result.setAttribute("list", commentsList);
			result.setAttribute("pager", pager);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			logger.info("in product comments list begin...");
		}catch(Exception e) {
			logger.error("in product comments list error:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		result.setAttribute("className", ProjectCommentsController.class.getName());
		result.setAttribute("methodName", "list");
		return result;
	}
	/** 放射 wap　过滤数据
	 * @param json
	 * @return
	 */
	public String waplist(String jsons){
		try {
			JSONObject json = JSON.parseObject(jsons);
			
			//获取  类名 方法名  进行放射
			JSONObject response=json.getJSONObject("attributes");
			JSONArray listArray = response.getJSONArray("list");
			for (int i = 0; i < listArray.size(); i++) {
				JSONObject jo = (JSONObject) listArray.get(i);
				//进行 多余字段过滤的数据
				jo.remove("createDate");
				jo.remove("createBy");
				jo.remove("updateDate");
				jo.remove("updateBy");
				jo.remove("uId");
				jo.remove("pcommentsContent");
				jo.remove("pcommentsType");
				jo.remove("pid");
				JSONObject joUser = (JSONObject) jo.get("user");
				joUser.remove("uNumberType");
				joUser.remove("uRole");
				joUser.remove("uTelephone");
				joUser.remove("uMobile");
				joUser.remove("uCode");
				joUser.remove("uEmail");
				joUser.remove("uSex");
				joUser.remove("uAge");
				joUser.remove("uAddress");
				joUser.remove("uRemark");
				joUser.remove("uType");
				joUser.remove("uAuditOpinion");
				joUser.remove("uCompany");
				joUser.remove("uManagerId");
				joUser.remove("faUserCertDTOList");
				joUser.remove("authenticationDTO");
				joUser.remove("forwardNum");
				joUser.remove("lastLoginIP");
				joUser.remove("lastLoginTime");
				joUser.remove("uRealName");
				joUser.remove("uCardAddress");
			}
			return json.toJSONString();
		} catch (Exception e) {
			log.error("error  product WAPlist:",e);
			return jsons;
		}
	}
	@RequestMapping(value="/add")
	@ResponseBody
	public ResultMap addPost( HttpServletRequest request, HttpServletResponse response,ProjectCommentsDTO commentsDTO){
		logger.info("in product comments add begin...");
		
		ResultMap result = new ResultMap();
		try {
			if(commentsDTO.getpId() == null){
				result.setStatus(STATUS.FAILE);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			
			Session session = SessionUtil.getSession();
			UserDTO loginUser = session.getUser();
			DTOdateSetUtil.setCreateInfo(commentsDTO);
			DTOdateSetUtil.setUpdateInfo(commentsDTO);
			commentsDTO.setuId(loginUser.getId());	
			commentsDTO.setpCommentsType(Constants.PROJECT_COMMENTS_BY_UGC);
			
			
			int i = projectCommentsService.addComments(commentsDTO);
			ProjectCommentsDTO bean = projectCommentsService.getComments(i);
			List<ProjectCommentsDTO> commentsList = new ArrayList<ProjectCommentsDTO>();
			commentsList.add(bean);
			Map<String,FaUserDTO> userMap = new HashMap<String,FaUserDTO>();
			for(ProjectCommentsDTO comments : commentsList){
				String uid = comments.getuId();
				if (!userMap.containsKey(uid)){
					
					FaUserDTO user = userService.getUserById(uid);
					user.setuLoginName(StringUtil.encryptCardNumber(user.getuLoginName()));
					userMap.put(uid, user);
				}
				comments.setUser(userMap.get(uid));
			}
			result.setAttribute("list", commentsList);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			logger.info("in product comments add end...");
		}catch(Exception e) {
			logger.error("in product comments add error:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
	
	@RequestMapping(value="/up")
	@ResponseBody
	public ResultMap up( HttpServletRequest request, HttpServletResponse response,ProjectCommentsLinkDTO linkDTO){
		logger.info("in product comments link UP begin...");
		ResultMap result = new ResultMap();
		try {
			//参数缺失
			if(linkDTO.getpCommentsId() == null){
				result.setStatus(STATUS.FAILE);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			//登录信息赋值
			Session session = SessionUtil.getSession();
			UserDTO loginUser = session.getUser();
			DTOdateSetUtil.setCreateInfo(linkDTO);
			DTOdateSetUtil.setUpdateInfo(linkDTO);
			linkDTO.setuId(loginUser.getId());	
			linkDTO.setLinkType(Constants.PROJECT_COMMENTS_LINK_TYPE_UP);
			ProjectCommentsLinkDTO linkBean = projectCommentsService.queryCommentsLink(linkDTO);
			if(linkBean != null){
				result.setStatus(ResultMap.STATUS.FAILE);
				result.setStatusCode(ResultMap.STATUS_CODE_PRODUCT_ALREADY_UP);
				return result;
			}
			//点赞逻辑
			boolean flag = projectCommentsService.upComments(linkDTO);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setAttribute("success", flag);
			logger.info("in product comments link UP end...");
		}catch(Exception e) {
			logger.error("comments  link UP error:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
}
